
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO


Namespace _2bMatesSite
	''' <summary>
	''' Summary description for join.
	''' </summary>
	Public Partial Class join
		Inherits BaseWebPage

		Private MIN_YEAR As Integer = Constants.MIN_YEAR
		Private MAX_YEAR As Integer = Constants.MAX_YEAR
		Protected chkFor1 As System.Web.UI.WebControls.CheckBox
		Protected chkFor2 As System.Web.UI.WebControls.CheckBox
		Protected chkFor3 As System.Web.UI.WebControls.CheckBox
		Protected chkFor4 As System.Web.UI.WebControls.CheckBox
		Protected chkFor5 As System.Web.UI.WebControls.CheckBox
		Protected chkFor6 As System.Web.UI.WebControls.CheckBox
		Protected lbl2BMatesWith As System.Web.UI.WebControls.Label
		Protected lblShowOnline As System.Web.UI.WebControls.Label

		Protected rbMen As System.Web.UI.WebControls.RadioButton
		Protected rbMenAndWomen As System.Web.UI.WebControls.RadioButton
		Protected rbWomen As System.Web.UI.WebControls.RadioButton
		Protected rbYes As System.Web.UI.WebControls.RadioButton
		Protected rbNo As System.Web.UI.WebControls.RadioButton
		Protected lblStatus As System.Web.UI.WebControls.Label
		Protected rbSDS As System.Web.UI.WebControls.RadioButton
		Protected rbInRelationship As System.Web.UI.WebControls.RadioButton
		Protected rbOpenRelationship As System.Web.UI.WebControls.RadioButton
		Protected rbMarried As System.Web.UI.WebControls.RadioButton
		Protected rbOpenMarriage As System.Web.UI.WebControls.RadioButton
		Protected lblAccSettingsLink As System.Web.UI.WebControls.Label
		Protected tblTopTable As System.Web.UI.WebControls.Table
		Protected divPrivacyVisibility As String = ""
		Protected rv2bMatesFor As System.Web.UI.WebControls.RequiredFieldValidator
		Protected lblCity As System.Web.UI.WebControls.Label
		Protected txtCity As System.Web.UI.WebControls.TextBox
		Protected lblPlaceOfBirth As System.Web.UI.WebControls.Label
		Protected txtPlaceOfBirth As System.Web.UI.WebControls.TextBox
		Protected divVisibility As String
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Auto login from an external link
			'AutoLogin( Request );

			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.JOIN_PAGE;

			Session.LCID = 1033
			Dim i As Integer
			Dim user As User = LoggedUser
			divVisibility = "display: none"

			Me.initPageTitles(Constants.JOIN, "", Form1, Nothing, Nothing)
			Me.initAdvertisements(Form1, Nothing)

			If Not Me.IsPostBack Then
				i = 1
				While i <= 31
					ddlDay.Items.Add(i.ToString())
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While
				' Fill in Months
				Dim months As String() = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", _
					"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
				i = 1
				While i <= 12
					ddlMonth.Items.Add(months(i - 1))
					ddlMonth.Items.FindByText(months(i - 1)).Value = i.ToString()
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While
				' Fill in Years
				i = MAX_YEAR
				While i >= MIN_YEAR
					ddlYear.Items.Add(i.ToString())
					System.Math.Max(System.Threading.Interlocked.Decrement(i),i + 1)
				End While

				Dim countriesCount As Integer = Constants.COUNTRIES.List.Count
				Dim tempCountry As Countries.Country

				i = 0
				While i < countriesCount
					tempCountry = DirectCast(Constants.COUNTRIES.List(i), Countries.Country)
					ddlCountry.Items.Add(tempCountry.Name)
					ddlCountry.Items.FindByText(tempCountry.Name).Value = tempCountry.ID.ToString()
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				ddlCountry.ClearSelection()
				ddlCountry.Items.FindByValue(Countries.UKID.ToString()).Selected = True
			End If

			' Try to get the referer, if any was passed
			Dim referer As BO.User = Nothing
			Dim refEmail As String = Request(Constants.REGISTRATION_PARAM_REFERER)
			If refEmail <> Nothing Then
				referer = Users.GetUserByEmail(refEmail)
			End If

			' If a referer was passed in the link - add it
			If referer <> Nothing Then
				txtReferrer.Text = referer.Email
				txtReferrer.Enabled = False
			End If

			litRightLinks.Text = "<a href='" + Constants.TESTIMONIALS + "'  class=""white_link"">Testimonials" + "</a> | " + "<A href='" + Constants.TELL_ME_MORE + "' class=""white_link"">Tell Me More</A> | <A href='" + Constants.DEMO + "' class=""white_link"">Demo!</A> | <A href='login.aspx' class=""white_link"">Login</A>"
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.cvTxtEmail.ServerValidate, AddressOf Me.cvCompareEmail_ServerValidate
			AddHandler Me.cvUserExist.ServerValidate, AddressOf Me.cvUserExist_ServerValidate
			AddHandler Me.cvZip.ServerValidate, AddressOf Me.cvZip_ServerValidate
			AddHandler Me.cvHtmlInZip.ServerValidate, AddressOf Me.cvHtmlInZip_ServerValidate
			AddHandler Me.cvHtmlInFirstName.ServerValidate, AddressOf Me.cvHtmlInFirstName_ServerValidate
			AddHandler Me.cvHtmlInLastName.ServerValidate, AddressOf Me.cvHtmlInLastName_ServerValidate
			AddHandler Me.cvHtmlInPassword.ServerValidate, AddressOf Me.cvHtmlInPassword_ServerValidate
			AddHandler Me.cvHtmlInPasswordAgain.ServerValidate, AddressOf Me.cvHtmlInPasswordAgain_ServerValidate

		End Sub
#End Region

		Protected Sub validateList(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
			args.IsValid = True
			If args.Value = "0" Then
				args.IsValid = False
			End If
		End Sub

		Protected Sub validateDate(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
			Dim day As String = ddlDay.SelectedItem.Value
			Dim month As String = ddlMonth.SelectedItem.Value
			Dim year As String = ddlYear.SelectedItem.Value
			If day <> "0" OrElse month <> "0" OrElse year <> "0" Then
				If Not isDate(day, month, year) Then
					args.IsValid = False
				Else
					args.IsValid = True
				End If
			End If
		End Sub

		Protected Function isDate(ByVal strDay As String, ByVal strMonth As String, ByVal strYear As String) As Boolean
			Dim validDate As Boolean = False

			Try
				' try creating the date
				New DateTime(Convert.ToInt32(strYear), Convert.ToInt32(strMonth), Convert.ToInt32(strDay))
				validDate = True
			Catch generatedExceptionName As Exception
			End Try

			Return validDate
		End Function

		Protected Sub GenerateDate(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
			Dim day As String = ddlDay.SelectedItem.Value
			Dim month As String = ddlMonth.SelectedItem.Value
			Dim year As String = ddlYear.SelectedItem.Value

			If day.Length = 1 Then
				day = "0" + day
			End If
			If month.Length = 1 Then
				month = "0" + month
			End If
			txtDateOfBirth.Text = month + "/" + day + "/" + year
			args.IsValid = True
		End Sub
		Protected Sub rbMale_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
			txtHiddenGender.Text = "1"
		End Sub

		Protected Sub rbFemale_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
			txtHiddenGender.Text = "2"
		End Sub

		Private Sub cvReferrer_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim referer As User = Users.GetUserByEmail(txtReferrer.Text)
			If referer <> Nothing Then
				args.IsValid = True
			Else
				args.IsValid = False
			End If
		End Sub

		Private Sub InitUser(ByVal user As User)
			Dim temp As String = ""

			temp = Request.Params(txtFirstName.UniqueID).Trim()
			user.Name = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS))

			temp = Request.Params(txtLastName.UniqueID).Trim()
			user.LastName = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS))
			


			user.Gender = Convert.ToInt32(Request.Params(txtHiddenGender.UniqueID))
			user.BirthDay = Convert.ToDateTime(txtDateOfBirth.Text)

			If Convert.ToInt32(Request.Params(ddlCountry.UniqueID).Trim()) <> -1 Then
				user.CountryID = Convert.ToInt32(Request.Params(ddlCountry.UniqueID).Trim())
			End If

			temp = Request.Params(txtZip.UniqueID).Trim()
			user.ZipCode = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_FIELD_ZIPCODE_CHARS))

			' set default gender preference
			'user.GenderPreference = Constants.GENDER_PREFERENCE_BOTH_ID;

			user.AboutMe = ""
			user.Affiliations = ""
			user.Books = ""
			user.City = ""
			user.Companies = ""
			user.EnableEmailNotify = True
			user.EnableGroupEmailNotify = True
			user.EnableNewsletter = True
			'user.Gender = -1;
			user.GenderPreference = -1
			user.MatesAP = False
			user.MatesDating = False
			user.MatesFriends = False
			user.MatesJHO = False
			user.MatesPen = False
			user.MatesSR = False
			user.Interests = ""
			user.LookingFor = ""
			user.MaritalStatus = 0
			user.Movies = ""
			user.Music = ""
			user.Occupation = ""
			user.PlaceOfBirth = ""
			user.Schools = ""
			user.ShowOnline = True
			user.SongURL1 = ""
			user.SongURL2 = ""
			user.SongURL3 = ""
			user.SongURLName1 = ""
			user.SongURLName2 = ""
			user.SongURLName3 = ""
			'			user.TemplateID = -1;
			user.TV = ""
			user.WAVURL = ""
			user.ApproveComments = True
			user.ApproveBlogComments = True
			user.ApproveFriends = True
			'user.ZipCode = "";

			user.Save()
		End Sub
		Protected Sub btnSignMeUp_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If Page.IsValid Then
				Dim user As BO.User = Nothing
				Dim referer As BO.User = Nothing
				Dim temp As String = ""
				Dim referrerValue As String = ""
				Dim emailValue As String = ""
				Dim passwordValue As String = ""

				temp = txtReferrer.Text.Trim()
				referrerValue = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS))

				temp = txtEmail.Text.Trim()
				emailValue = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS))
				'emailValue = emailValue.ToLower();

				temp = txtPassword.Text.Trim()
				passwordValue = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_FIELD_PASSWORD_MAX_CHARS))

				' init referrer
				If referrerValue <> "" Then
					referer = BO.Users.GetUserByEmail(referrerValue)
				End If

				user = Users.GetUserByEmail(emailValue)

				If user <> Nothing AndAlso user.Status = Users.STATUS_LOCKED Then
					' user has previously canceled his account
					user.SetUnconfirmed()
					If referer <> Nothing AndAlso referer.Status <> Users.STATUS_LOCKED Then
							'user.Save();
						user.RefererID = referer.ID
					Else
							'user.Save();
						user.RefererID = -1
					End If
				Else
					If referer <> Nothing AndAlso referer.Status <> Users.STATUS_LOCKED Then
						user = New BO.User(emailValue, passwordValue, referer.ID)
					Else
						user = New BO.User(emailValue, passwordValue)
					End If
				End If
				InitUser(user)

				If referer <> Nothing AndAlso referer.Status <> Users.STATUS_LOCKED Then
					user.AcceptCreateFriendship(referer.ID)
				End If

				BO.Users.SendRegistraionConfirmation(Constants.APPLICATION_URL + "/" + Constants.REGISTRATION_CONFIRM_PAGE, user)

				' log in the user
				user = Users.Login(emailValue, passwordValue)
				LoggedUser = user
				SendWelcomeMessage(LoggedUser)
				Response.Redirect(Constants.INVITE_FRIENDS_PAGE + "?" + Constants.REGISTRATION_PARAM + "=" + Constants.REGISTRATION_VALUE)
			End If
		End Sub

		Private Sub SendWelcomeMessage(ByVal usr As User)
			If usr <> Nothing Then
				Dim subject As String = ""
				Dim body As String = ""

				subject = Constants.WELCOME_MESSAGE_SUBJECT
				body = "Hi " + usr.Name + "," + Constants.WELCOME_MESSAGE_BODY
				'Request.Params[txtMessage_bulletin.UniqueID].Trim();
				subject = subject.Substring(0, Math.Min(subject.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS))
				body = body.Substring(0, Math.Min(body.Length, Constants.TEXT_AREA_LONG_MESSAGES_MAX_CHARS))

				Dim sender As User = Users.GetUserByEmail(Constants.WELCOME_MESSAGE_SENDER)
				Dim msgNew As New Message(sender.ID, usr.ID, subject, body)
				msgNew.SendWelcome()
			End If
		End Sub

		Private Sub cvZip_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim countryID As Integer = Convert.ToInt32(Request.Params(ddlCountry.UniqueID))
			If (countryID = Countries.USAID OrElse countryID = Countries.CANADAID) AndAlso Request.Params(txtZip.UniqueID) = "" Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		Private Sub cvHtmlInFirstName_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim fn As String = Request.Form(txtFirstName.UniqueID)

			If isHTML(fn) Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		Private Sub cvHtmlInLastName_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim ln As String = Request.Form(txtLastName.UniqueID)

			If isHTML(ln) Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		Private Sub cvHtmlInZip_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim zip As String = Request.Form(txtZip.UniqueID)

			If isHTML(zip) Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		Private Sub cvHtmlInPassword_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim pass As String = Request.Form(txtPassword.UniqueID)

			If isHTML(pass) Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		Private Sub cvHtmlInPasswordAgain_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim pass As String = Request.Form(txtPasswordAgain.UniqueID)

			If isHTML(pass) Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		Private Sub cvUserExist_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim temp As String
			Dim emailValue As String = ""

			temp = txtEmail.Text.Trim()
			emailValue = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS))

			Dim usr As User = Users.GetUserByEmail(emailValue)
			If usr <> Nothing Then
				args.IsValid = False
				If usr.Status = Users.STATUS_LOCKED Then
					args.IsValid = True
				End If
			Else
				args.IsValid = True
			End If
		End Sub

		Private Sub cvCompareEmail_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim email As String = txtEmail.Text.Trim().ToLower()
			Dim emailToCompare As String = txtVerifyEmail.Text.Trim().ToLower()

			If (email = String.Empty) OrElse (email <> emailToCompare) Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

	End Class
End Namespace







